package ca.eloas.judging.rest.repositories;

import ca.eloas.judging.model.CompetitionObject;
import ca.eloas.judging.model.JudgeObject;
import ca.eloas.judging.model.ScoreObject;
import ca.eloas.judging.model.TeamObject;

import javax.inject.Singleton;

/**
 * @author JP
 */
@Singleton
public class OfyScoreRepository extends AbstractOfyRepository<ScoreObject> implements ScoreRepository {

    public OfyScoreRepository() {
        super(ScoreObject.class);
    }


    public Iterable<ScoreObject> getScoreFromJudge(CompetitionObject competition, JudgeObject judge) {

        return getPersistence().load().type(ScoreObject.class).filter("judge", judge).iterable();
    }

    public ScoreObject getScoreForJudge(TeamObject team, JudgeObject judge) {

        System.err.println("Looking for judge " + judge.getId());
        for (ScoreObject scoreObject : getPersistence().load().type(ScoreObject.class).ancestor(team).iterable()) {

            System.err.println("score " + scoreObject.getId() + " with judge " + scoreObject.getJudge().getId());
        }

        return getPersistence().load().type(ScoreObject.class).ancestor(team).filter("judge", judge).first().get();
    }
}
